package com.snake.springboot.authority.web.controller;

import com.snake.springboot.authority.api.model.AuthBuyStrategyModel;
import com.snake.springboot.authority.api.model.AuthOperatorStrategyModel;
import com.snake.springboot.authority.api.request.AuthOperatorStrategyQuery;
import com.snake.springboot.authority.api.request.AuthorizationDefaultSave;
import com.snake.springboot.authority.api.response.AuthOperatorStrategyRsp;
import com.snake.springboot.authority.api.response.OperatorStrategySumRsp;
import com.snake.springboot.authority.api.service.AuthorizationStrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.snake.common.result.ResponseResult;
import org.snake.common.result.ResponseResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类 {@code AuthorizationStrategyController}  <br> .
 * <p>权限策略授权控制器
 * <p>创建时间：2025/02/08
 *
 * @author Huang.yj
 */
@Api(tags = "权限管理-权限策略授权")
@RestController
@RequestMapping("/auth/strategy-authorization")
public class AuthorizationStrategyController {

    private AuthorizationStrategyService authorizationStrategyService;

    @Autowired
    public AuthorizationStrategyController(AuthorizationStrategyService authorizationStrategyService){
        this.authorizationStrategyService = authorizationStrategyService;
    }

    @ApiOperation("授权全部权限策略")
    @PostMapping("/authorization")
    public ResponseResult<Object> authorization(
            @ApiParam(name = "操作员与权限策略关系列表", value = "传入json格式", required = true)
            @RequestBody List<AuthOperatorStrategyModel> models){
        authorizationStrategyService.authorization(models);
        return ResponseResultFactory.ucc("操作成功");
    }

    @ApiOperation("授权部分权限策略")
    @PostMapping("/some-author")
    public ResponseResult<Object> someAuthor(
            @ApiParam(name = "操作员与权限策略关系列表", value = "传入json格式", required = true)
            @RequestBody List<AuthOperatorStrategyModel> models){
        authorizationStrategyService.someAuthor(models);
        return ResponseResultFactory.ucc("操作成功");
    }

    @ApiOperation("取消部分权限策略")
    @PostMapping("/cancel-some-author")
    public ResponseResult<Object> cancelSomeAuthor(
            @ApiParam(name = "操作员与权限策略关系列表", value = "传入json格式", required = true)
            @RequestBody List<AuthOperatorStrategyModel> models){
        authorizationStrategyService.cancelSomeAuthor(models);
        return ResponseResultFactory.ucc("操作成功");
    }

    @ApiOperation("授权购买权限策略-服务内部调用")
    @PostMapping("/buy-author-inner")
    public ResponseResult<Object> buyAuthorInner(
            @ApiParam(name = "操作员与权限策略关系列表", value = "传入json格式", required = true)
            @RequestBody List<AuthBuyStrategyModel> models){
        authorizationStrategyService.buyAuthor(models);
        return ResponseResultFactory.ucc("操作成功");
    }

    @ApiOperation("取消购买权限策略-服务内部调用")
    @PostMapping("/cancel-buy-author-inner")
    public ResponseResult<Object> cancelBuyAuthorInner(
            @ApiParam(name = "操作员与权限策略关系列表", value = "传入json格式", required = true)
            @RequestBody List<AuthBuyStrategyModel> models){
        authorizationStrategyService.cancelBuyAuthor(models);
        return ResponseResultFactory.ucc("操作成功");
    }


    @ApiOperation("取消全部授权权限策略")
    @GetMapping("/cancel-authorization/{operatorid}")
    public ResponseResult<Object> cancelAuthorization(
            @ApiParam(name = "操作员id", value = "传入string格式", required = true)
            @PathVariable("operatorid") String operatorId){
        authorizationStrategyService.cancelAuthorization(operatorId);
        return ResponseResultFactory.ucc("操作成功");
    }

    @ApiOperation("查询操作员已授权权限策略")
    @PostMapping("/query/selected-strategy")
    public ResponseResult<List<AuthOperatorStrategyRsp>> queryOperatorStrategy(
            @ApiParam(name = "操作员id", value = "传入string格式", required = true)
            @RequestBody AuthOperatorStrategyQuery query){
        List<AuthOperatorStrategyRsp> result = authorizationStrategyService.queryOperatorStrategy(query);
        return ResponseResultFactory.ucc("查询成功",result);
    }

    @ApiOperation("管理员查询已授权权限策略")
    @PostMapping("/admin/selected-strategy")
    public ResponseResult<List<AuthOperatorStrategyRsp>> queryAdminStrategy(
            @ApiParam(name = "操作员id", value = "传入string格式", required = true)
            @RequestBody AuthOperatorStrategyQuery query){
        List<AuthOperatorStrategyRsp> result = authorizationStrategyService.queryOperatorStrategy(query);
        return ResponseResultFactory.ucc("查询成功",result);
    }

    @ApiOperation("授权默认的权限策略")
    @PostMapping("/authorization-default")
    public ResponseResult<Object> authorizationDefault(
            @ApiParam(name = "操作员与权限策略关系列表", value = "传入json格式", required = true)
            @RequestBody AuthorizationDefaultSave model){
        authorizationStrategyService.authorizationDefault(model);
        return ResponseResultFactory.ucc("操作成功");
    }

    @ApiOperation("查询策略统计")
    @PostMapping("/strategy-sum")
    public ResponseResult<List<OperatorStrategySumRsp>> strategySum(
            @ApiParam(name = "操作人IDs", value = "传入json格式", required = true)
            @RequestBody List<String> operatorIds){
        List<OperatorStrategySumRsp> sumRspList = authorizationStrategyService.strategySum(operatorIds);
        return ResponseResultFactory.ucc("操作成功",sumRspList);
    }


}
